package com.more.spring.boot;


import com.more.spring.boot.mapper.UserMapper;
import com.more.spring.boot.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class SqlSessoinTest2 {
    public static void main(String[] args) throws IOException {
        //获取输入流
        try {
            //根据XML构建SqlSessionFactory
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            //获取sqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();

            //获取mapper
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> users = mapper.selectAllUser();

            for (User user : users) {
                System.out.println(user);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
